// 用于创建一个默认的admin用户
// email 1925541533@qq.com
// password 123456
// xuehao 123456789

const { User } = require("../mod/user")
const { Work } = require("../mod/work")

const { createWorks } = require("./tool")
const {
    setCrypto
} = require('../tool/crypto.js')

const { Tool } = require("../mod/tool")

const { Jurisdiction } = require("../mod/jurisdiction")

const { adminRouter,
    teacherRouter,
    studentRouter } = require("../tool/tool")

let userdata = {
    shengfen: 2,
    email: '1925541533@qq.com',
    xuehao: '123456789',
    password: '123456'
}
userdata.password = setCrypto(userdata.password)

async function createAdmin() {
    
    // 创建默认的权限列表

    let routerlist_number = await Jurisdiction.find().countDocuments()
    // console.log(routerlist_number)
    if (routerlist_number == 0) {
        new Jurisdiction({
            user_grade: 0,
            jurisdiction_routerlist: studentRouter
        }).save()
        new Jurisdiction({
            user_grade: 1,
            jurisdiction_routerlist: teacherRouter
        }).save()
        new Jurisdiction({
            user_grade: 2,
            jurisdiction_routerlist: adminRouter
        }).save()

        console.log("创建了基本的路由列表")
    }


    // 先查找admin用户是否已经创建
    let data = await User.findOne(userdata)
    if (data == null) {
        let routerdata = await Jurisdiction.findOne({ user_grade: userdata.shengfen })
        userdata.user_jurisdiction_routerlist = routerdata.jurisdiction_routerlist
        let data1 = await new User(userdata).save()
        if (data1) {
            console.log(`创建默认admin用户成功，xuehao:${userdata.xuehao}，password:123456`)
        }
    }

    // 创建基本的院系结构
    let stract = await Tool.find()
    if (stract.length == 0) {
        let data = await new Tool().save()
        if (data) {
            console.log("创建基本院系结构成功")
        }
    }

    // 创建默认的假数据---就业
    let count = await Work.find().countDocuments()
    if (count > 20000) {      
        console.log("基础数据已经足够了")    
    } else {
        createWorks()
    }


}

createAdmin()
